﻿using System;
using WatiN.Core;
using System.Threading;
using WatiN.Core.Interfaces;
using AttributeConstraint=WatiN.Core.Constraints.AttributeConstraint;

namespace WatiNCF.Extensions
{
    public static class ElementExtensions
    {
        /// <summary>
        /// Waits until the specified element is enabled within the web page.
        /// </summary>
        /// <param name="element">The element.</param>
        public static T WaitUntilEnabled<T>(this IElement element) where T : class
        {
            var timer = new SimpleTimer(IE.Settings.WaitForCompleteTimeOut);

            do
            {
                var value = element.GetAttributeValue("disabled");
                
                if (value != "True" && 
                    value != "true")
                {
                    return element as T;
                }
                Thread.Sleep(200);
            }
            while (!timer.Elapsed);

            throw new TimeoutException("Waited too long for " + element.Id + " to become enabled");
        }

        /// <summary>
        /// Waits until the specified element is enabled within the web page.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="element">The element.</param>
        /// <param name="timeToWait">The time to wait in milliseconds.</param>
        /// <returns></returns>
        public static T Wait<T>(this IElement element, int timeToWait) where T : class
        {
            Thread.Sleep(timeToWait);
            return element as T;
        }

        /// <summary>
        /// Checks whether the element exists, if so performs the specified action,
        /// otherwise just returns the current element object.
        /// </summary>
        /// <typeparam name="T">The type of element to check for existence.</typeparam>
        /// <param name="element">The element.</param>
        /// <param name="action">The action.</param>
        /// <returns></returns>
        public static T IfExists<T>(this IElement element, Action<T> action) where T : class
        {
            if (element != null && element.Exists)
            {
                action(element as T);
            }

            return element as T;
        }

        public static ITextField TypeText(this ITextField textField, string text)
        {
            if (textField != null)
            {
                textField.Value = text;
            }
            return textField;
        }

        public static ITextField AppendText(this ITextField textField, string text)
        {
            if (textField != null)
            {
                textField.Value += text;
            }
            return textField;
        }

        public static Table Table(this IDiv div, AttributeConstraint findConstraint)
        {
            Table table = null;
            if(div is Div)
            {
                table = ((Div)div).Table(findConstraint);
            }
            return table;
        }
    }
}
